package com.guoyu.project.web.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Banner;
import com.guoyu.project.model.College_Company;
import com.guoyu.project.model.Project_Dynamic;
import com.guoyu.project.model.Project_Manage;
import com.guoyu.project.service.FileUploadService;
import com.guoyu.project.service.Project_DynamicService;

@Controller
@RequestMapping("api/PDynamic")
public class ProjectDynamicApiController {
	
	    //日志
	    private final static Logger logger = LoggerFactory.getLogger(ProjectDynamicApiController.class);

	    @Resource
	    private Project_DynamicService project_DynamicService;
	    
	    @Resource
	    private FileUploadService fileUploadService;

	    //根据项目获取项目动态列表
	    @RequestMapping("dynamic")
	    @ResponseBody
	    public APIResponse dynamic(int id,Model model,@RequestParam(defaultValue = "0") int offset,
	            @RequestParam(defaultValue = "10") int limit) {
	    	Project_Dynamic dynamic=new Project_Dynamic();
	    	dynamic.setPmanage_id(id);
	    	Map<String, Object> params = Maps.newHashMap();
	    	params.put("dynamic", dynamic);
	    	params.put("pmanage_id", dynamic.getPmanage_id());
	    	Pagination<Project_Dynamic> pagination = project_DynamicService.queryBypmanageId(offset, limit, params);
	    	 return APIResponse.returnSuccess(pagination);
	    }
	    
	    /**
	     * 保存今日资讯信息
	     *
	     * @return
	     */
	    @RequestMapping("save")
	    @ResponseBody
	    public APIResponse save(@RequestParam(value = "picPD", required = false) MultipartFile picture,
	    		@RequestParam(value = "saveFil1", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "saveFil2", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "saveFil3", required = false)MultipartFile thFile,
	    		int pmanage_id,
	    		Project_Dynamic project_dynamic) {
	        logger.info("提交添加信息：{}", JsonUtil.encode(project_dynamic));
	        
	        //图片上传
	        String picPath = fileUploadService.upload(picture);
	        project_dynamic.setPicture(picPath);
	        //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_dynamic.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_dynamic.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_dynamic.setThFile(thfilePath);
	        //设置默认状态  
	        project_dynamic.setStatus(0);
	        project_dynamic.setPmanage_id(pmanage_id);
	        project_DynamicService.saveProject_Dyanmic(project_dynamic);
	        return APIResponse.returnSuccess();
	    }

	    //根据id获取对象
	    @RequestMapping("query")
	    @ResponseBody
	    public APIResponse edit(Integer pd_id) {
	        Project_Dynamic projectDynamic = project_DynamicService.queryById(pd_id);
	        return APIResponse.returnSuccess(projectDynamic);
	    }
	    
	    /**
	     * 更新
	     *
	     * @return
	     */
	    @RequestMapping("update")
	    @ResponseBody
	    public APIResponse update(@RequestParam(value = "picPD", required = false) MultipartFile picture,
	    		@RequestParam(value = "updateFil1", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "updateFil2", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "updateFil3", required = false)MultipartFile thFile,
	    		Project_Dynamic project_dynamic) {
	        logger.info("提交修改项目管理信息：{}", JsonUtil.encode(project_dynamic));
	      //图片上传
	        String picPath = fileUploadService.upload(picture);
	        project_dynamic.setPicture(picPath);
	        //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_dynamic.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_dynamic.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_dynamic.setThFile(thfilePath);
	        
	        project_DynamicService.updateProject_Dyanmic(project_dynamic);
	        return APIResponse.returnSuccess();
	    }

	    /**
	     * 删除
	     *
	     * @return
	     */
	    @RequestMapping("delete")
	    @ResponseBody
	    public APIResponse delete(Integer pd_id) {
	    	project_DynamicService.deleteById(pd_id);
	        return APIResponse.returnSuccess();
	    } 
	    
	    

	    
	}